<html>
<head><meta charset="utf-8"><title>reusable code analyzer · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html">reusable code analyzer</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="194027227"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/194027227" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#194027227">(Apr 15 2020 at 13:13)</a>:</h4>
<p>So, I have been recently thinking of trying to come up with a rust code analyzer for one of my projects and I am wondering if I could adopt rust-analyzer code.  I need to analyze only the actual code (not deps), I probably want to edit some stuff and reconstruct the code back again (so need to preserve whitespace in the AST), I need the types of stuff so that analysis can be done on types too.</p>



<a name="194030608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/194030608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#194030608">(Apr 15 2020 at 13:34)</a>:</h4>
<p>I suggest trying to fork rust-analyzer, and implement the analysis as an assist</p>



<a name="194030639"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/194030639" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#194030639">(Apr 15 2020 at 13:34)</a>:</h4>
<p>That's the easiest way to experiment with analysis and transformation</p>



<a name="194030932"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/194030932" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#194030932">(Apr 15 2020 at 13:36)</a>:</h4>
<p>Thanks <span class="user-mention" data-user-id="133169">@matklad</span> . Will do that.</p>



<a name="194690697"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/194690697" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#194690697">(Apr 20 2020 at 15:24)</a>:</h4>
<p><span class="user-mention" data-user-id="133169">@matklad</span>  I started trying to do this and as far as I understand, assists need the cursor to be somewhere. I want to do a visitor kind of thing for the tree. Do I continue with this part of the code by changing stuff? Or should I explore other way I can do this?</p>



<a name="194691200"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/194691200" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#194691200">(Apr 20 2020 at 15:27)</a>:</h4>
<p>You are correct, that assist starts from a cursor. </p>
<p>Still, I would start with an assist, to see if the underlying analysis/editing API is enough for your use-case. If it is enough, it should be possible to re-rail the implementation onto a visitor. But we currently don't have a vistor-based infra, so it would be significantly more work to add it</p>



<a name="194691453"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/194691453" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#194691453">(Apr 20 2020 at 15:28)</a>:</h4>
<p>Yeah, that is exactly what I was thinking. Looks like the editing API looks enough (or easy to enhance). I will work on this a bit more. And this is also making me understand RA code more which means I can contribute some fixes to IDE issues that were annoying me :D</p>



<a name="194692047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/194692047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#194692047">(Apr 20 2020 at 15:32)</a>:</h4>
<p>Actually, I think our assist infra could use some improvement. </p>
<p>Running assists/inspections in batch on the code base is something we want to do eventually. And for that, we need an API which can work both from text cursor and from tree visitor. Good food for thought, thanks!</p>



<a name="194692411"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/194692411" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#194692411">(Apr 20 2020 at 15:34)</a>:</h4>
<p>I agree. Basically, we are working on releasing Clap v3. And I want to create a tool that would automate the upgrade from v2 to v3 so that users can just run one command and their code is now upgraded to use clap v3. <a href="https://github.com/pksunkara/cargo-up" title="https://github.com/pksunkara/cargo-up">https://github.com/pksunkara/cargo-up</a>. And this can be made generic for all crates</p>



<a name="194692802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/194692802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#194692802">(Apr 20 2020 at 15:36)</a>:</h4>
<p>Ah yeah, long term I'd really love to see kotlin-style reprecated, replaceWith to automate library upgrades.</p>



<a name="194693135"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/194693135" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#194693135">(Apr 20 2020 at 15:39)</a>:</h4>
<p>At the moment, I think it would be ok to pin rust-analyzer to a specific commit and implement such a tool on top of a "frozen" API. Sadly, I think there currently isn't an API stable enough to guaranteed that the tool would work across different ra versions. </p>
<p>You might also take a look at <a href="https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_ide/src/ssr.rs" title="https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_ide/src/ssr.rs">ssr</a>, which might be relevant to your use-case</p>



<a name="194694901"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/194694901" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#194694901">(Apr 20 2020 at 15:51)</a>:</h4>
<p>Another exapmle of tree-walking is here: <a href="https://github.com/rust-analyzer/rust-analyzer/blob/2e0b7b0159ed922693db48f3f94ed95b1827494a/crates/ra_ide/src/diagnostics.rs#L45-L48" title="https://github.com/rust-analyzer/rust-analyzer/blob/2e0b7b0159ed922693db48f3f94ed95b1827494a/crates/ra_ide/src/diagnostics.rs#L45-L48">https://github.com/rust-analyzer/rust-analyzer/blob/2e0b7b0159ed922693db48f3f94ed95b1827494a/crates/ra_ide/src/diagnostics.rs#L45-L48</a></p>



<a name="195174113"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/195174113" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#195174113">(Apr 24 2020 at 11:17)</a>:</h4>
<p>I was talking about this to someone else and wanted to also paste this here.</p>
<p>So, I am trying to build a small visitor for code analysis. Visitor will be working on code that is written as is. So, macros will not be expanded. But you will also have access to the types of every expression, item, etc.. (which is resolved after expanding macros). <code>rust-analyzer</code> have the syntax AST ready, and it also have types information for the whole project.</p>
<p>Why the type information is important for the analysis? Regarding the <code>cargo-up</code> I linked above, let's assume <code>clap::Arg</code> has changed it's method <code>help</code> to <code>about</code>. If we are running the tool on the following code:</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">clap</span>::<span class="n">Arg</span>::<span class="n">new</span><span class="p">(</span><span class="s">&quot;a&quot;</span><span class="p">);</span><span class="w"></span>

<span class="n">a</span><span class="p">.</span><span class="n">help</span><span class="p">(</span><span class="s">&quot;help message&quot;</span><span class="p">);</span><span class="w"></span>
</pre></div>


<p>Without the type information, you can't recognise that <code>help</code> needs to be changed to <code>about</code>. With type info, we know that <code>a</code> is of type <code>clap::Arg</code>. So, the <code>cargo-up</code> visitor when visiting <code>method_call</code> can be something like this:</p>
<div class="codehilite"><pre><span></span><span class="k">if</span><span class="w"> </span><span class="n">method</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s">&quot;help&quot;</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">expr</span><span class="p">.</span><span class="k">type</span> <span class="o">==</span><span class="w"> </span><span class="s">&quot;clap::Arg&quot;</span><span class="w">  </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">method</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;about&quot;</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>I am currently trying to get a small MVP of the visitor working</p>



<a name="195175409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/195175409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#195175409">(Apr 24 2020 at 11:34)</a>:</h4>
<p>we also provide the actual resolved method, using that would be more accurate than checking name and type in some edge cases</p>



<a name="195175907"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/195175907" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#195175907">(Apr 24 2020 at 11:40)</a>:</h4>
<p>actually not even edge cases, your approach would already have problems with autoref/deref (i.e. what if it's a <code>&amp;self</code> method and the expression type is <code>&amp;clap::Arg</code>? what if the expression type is <code>&amp;&amp;&amp;clap::Arg</code> or <code>Arc&lt;clap::Arg&gt;</code>?)</p>



<a name="195176069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/195176069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#195176069">(Apr 24 2020 at 11:42)</a>:</h4>
<p>That's actually really helpful. I am still going through the rust-analyzer code. I would appreciate it if you could point that stuff out</p>



<a name="195176498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/195176498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#195176498">(Apr 24 2020 at 11:47)</a>:</h4>
<p>in the <code>Semantics</code> API, you've got <a href="https://github.com/rust-analyzer/rust-analyzer/blob/edd6f91cab77c4fabf449a50f4f65e4761c98ca4/crates/ra_hir/src/semantics.rs#L183" title="https://github.com/rust-analyzer/rust-analyzer/blob/edd6f91cab77c4fabf449a50f4f65e4761c98ca4/crates/ra_hir/src/semantics.rs#L183"><code>resolve_method_call</code></a>, which will give you the resolved <code>Function</code>, so then you'd just have to have some way of getting the <code>Function</code> you actually want to replace</p>



<a name="196245373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196245373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196245373">(May 04 2020 at 22:03)</a>:</h4>
<p><span class="user-mention" data-user-id="133169">@matklad</span> I got a basic visitor working, but unfortunately I am stuck trying to load the semantics. Looks like <code>load_cargo</code> which I am using to load the code isn't loading <code>library_roots</code>. Really appreciate some direction here</p>



<a name="196280719"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196280719" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196280719">(May 05 2020 at 08:38)</a>:</h4>
<p><span class="user-mention" data-user-id="254853">@pksunkara</span> I'd probably start with looking <code>analysis_stats</code> -- it also uses <code>load_cargo</code>, and it does work with dependencies</p>



<a name="196280963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196280963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196280963">(May 05 2020 at 08:41)</a>:</h4>
<p>Okay, thanks</p>



<a name="196594661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196594661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196594661">(May 06 2020 at 07:06)</a>:</h4>
<p>So, <code>load_cargo</code> does load the dependency source roots, but it doesn't set them as <code>library_roots</code> which led me to the original confusion. <code>library_roots</code> is only set in the <code>WorldState</code> related logic. I got the semantics working though, turns out I need to do an additional <code>semantics.parse</code> for the file I am visiting.</p>



<a name="196600554"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196600554" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196600554">(May 06 2020 at 08:11)</a>:</h4>
<p><span class="user-mention" data-user-id="129457">@Florian Diebold</span> Hey, thanks for the help. I was able to get the module name <code>clap::arg</code> and fn name <code>help</code> from the <code>Function</code>, but I am stumped on how to get the struct <code>Arg</code> for it, since the function is a method call on <code>clap::arg::Arg</code>. Am I missing something?</p>



<a name="196600972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196600972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196600972">(May 06 2020 at 08:13)</a>:</h4>
<p><span class="user-mention" data-user-id="254853">@pksunkara</span> is this while looking at the method call, or at the definition? i.e. why do you need to get the struct?</p>



<a name="196600999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196600999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196600999">(May 06 2020 at 08:13)</a>:</h4>
<p>While looking at the method call. I want to get the full path of the definition</p>



<a name="196601142"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196601142" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196601142">(May 06 2020 at 08:14)</a>:</h4>
<p>There might be <code>clap::arg::Arg::help(&amp;self)</code> and <code>clap::arg::Arg2::help(&amp;self)</code>. How would I differentiate?</p>



<a name="196601246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196601246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196601246">(May 06 2020 at 08:16)</a>:</h4>
<p>I am looking at how the hover hints in vscode work. If I hover on method_call, they have the full path (including the struct) and then the function docs.</p>



<a name="196601534"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196601534" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196601534">(May 06 2020 at 08:19)</a>:</h4>
<p>my suggestion would be to find the <code>Function</code> you want to replace first, for example by walking down the modules in the clap crate to the impl and looking for the function in the impl by name. And then you can just compare the resolved <code>Function</code> with the one you looked up</p>



<a name="196601680"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196601680" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196601680">(May 06 2020 at 08:20)</a>:</h4>
<p>but you can also go from the <code>Function</code> to the containing <code>ImplDef</code> using <code>as_assoc_item</code> and then <code>container</code></p>



<a name="196601772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196601772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196601772">(May 06 2020 at 08:21)</a>:</h4>
<p>and then the <code>ImplDef</code> has a <code>target_ty</code></p>



<a name="196601774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196601774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196601774">(May 06 2020 at 08:21)</a>:</h4>
<blockquote>
<p>walking down the modules in the clap crate to the impl and looking for the function in the impl by name</p>
</blockquote>
<p>I would prefer not to do it because we are not actually changing the libraries code, but only the user written code</p>



<a name="196601803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196601803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196601803">(May 06 2020 at 08:21)</a>:</h4>
<p>But if I need a reference to the original implementation so that I can compare them, I guess I can do it.</p>



<a name="196601811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196601811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196601811">(May 06 2020 at 08:21)</a>:</h4>
<p>I don't mean changing that code, just finding the right <code>Function</code> so you know what you're looking for</p>



<a name="196601945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196601945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196601945">(May 06 2020 at 08:23)</a>:</h4>
<p>if you go from the resolved <code>Function</code> to the impl to the type, you still have to determine somehow whether that's the type you were looking for</p>



<a name="196601996"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196601996" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196601996">(May 06 2020 at 08:24)</a>:</h4>
<blockquote>
<p>you still have to determine somehow whether that's the type you were looking for</p>
</blockquote>
<p>I don't think that's an issue, because I am doing it inside a visitor pattern and this feels correct way to me</p>



<a name="196602212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196602212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196602212">(May 06 2020 at 08:26)</a>:</h4>
<p>I mean, finding the type to compare to is just about as complicated as finding the function, you've just taken more steps to get there</p>



<a name="196602415"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196602415" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196602415">(May 06 2020 at 08:29)</a>:</h4>
<p>Definitely, both approaches work. I will keep it in mind. Thanks for the help.</p>



<a name="196620759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/reusable%20code%20analyzer/near/196620759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/reusable.20code.20analyzer.html#196620759">(May 06 2020 at 11:33)</a>:</h4>
<p><span class="user-mention" data-user-id="133169">@matklad</span> Got this successfully working with minor visibility changes in rust_analyzer. <a href="https://github.com/pksunkara/cargo-up/blob/master/src/main.rs#L12" title="https://github.com/pksunkara/cargo-up/blob/master/src/main.rs#L12">https://github.com/pksunkara/cargo-up/blob/master/src/main.rs#L12</a>. What do you want me to do? Should I send PRs to add <code>ra_visitor</code>?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>